
package com.schedualnew.mq.util;

import com.alibaba.fastjson.JSONPath;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.XML;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class CommonUtil {
    private static final Logger log;

    public static List<String> split2List(final String str) {
        final List<String> list = new ArrayList<String>();
        if (StringUtils.isNotBlank(str)) {
            String[] arr = str.split(",");
            if (arr.length <= 1) {
                arr = str.split("\\&");
            }
            for (final String tmp : arr) {
                list.add(tmp);
            }
        }
        return list;
    }

    public static List<String> strToList(final String str, final String point) {
        final List<String> list = new ArrayList<String>();
        if (StringUtils.isNotBlank(str)) {
            final String[] split;
            final String[] arr = split = str.split(point);
            for (final String tmp : split) {
                list.add(tmp);
            }
        }
        return list;
    }

    public static String turnStatus(final String status) {
        if (status == null) {
            return "0";
        }
        switch (status) {
            case "01": {
                return "1";
            }
            case "02": {
                return "5";
            }
            case "04": {
                return "6";
            }
            case "06":
            case "08": {
                return "2";
            }
            case "10": {
                return "3";
            }
            default: {
                return status;
            }
        }
    }

    public static String status2Text(final String status) {
        if (status == null) {
            return "未预约";
        }
        switch (status) {
            case "01": {
                return "已锁定";
            }
            case "02": {
                return "已申请";
            }
            case "04": {
                return "已退回";
            }
            case "06":
            case "08": {
                return "已预约";
            }
            case "10": {
                return "已报到";
            }
            case "40": {
                return "已检查";
            }
            case "60": {
                return "初步报告";
            }
            case "70": {
                return "确认报告";
            }
            default: {
                return "未预约";
            }
        }
    }

    public static String convertItemFlagsCondition(final String itemFlag) {
        if (itemFlag == null) {
            return null;
        }
        return convertItemFlags(itemFlag).replace("0", "_");
    }

    public static String convertItemFlags(final String itemFlag) {
        if (itemFlag == null) {
            return null;
        }
        final StringBuilder flags = new StringBuilder("00000000000000000000");
        final List<String> itemFlags = split2List(itemFlag);
        if (itemFlags.contains("0")) {
            flags.replace(0, 1, "1");
        }
        if (itemFlags.contains("1")) {
            flags.replace(1, 2, "1");
        }
        if (itemFlags.contains("2")) {
            flags.replace(2, 3, "1");
        }
        if (itemFlags.contains("3")) {
            flags.replace(3, 4, "1");
        }
        if (itemFlags.contains("4")) {
            flags.replace(4, 5, "1");
        }
        if (itemFlags.contains("5")) {
            flags.replace(5, 6, "1");
        }
        if (itemFlags.contains("6")) {
            flags.replace(6, 7, "1");
        }
        if (itemFlags.contains("7")) {
            flags.replace(0, 1, "2");
        }
        if (itemFlags.contains("8")) {
            flags.replace(7, 8, "1");
        }
        if (itemFlags.contains("9")) {
            flags.replace(8, 9, "1");
        }
        if (itemFlags.contains("21")) {
            flags.replace(8, 9, "2");
        }
        if (itemFlags.contains("22")) {
            flags.replace(8, 9, "3");
        }
        if (itemFlags.contains("10")) {
            flags.replace(9, 10, "1");
        }
        if (itemFlags.contains("11")) {
            flags.replace(10, 11, "1");
        }
        if (itemFlags.contains("12")) {
            flags.replace(11, 12, "1");
        }
        if (itemFlags.contains("13")) {
            flags.replace(11, 12, "2");
        }
        return flags.toString();
    }

    public static String convertFlagsCondition(final String applyFlag) {
        if (applyFlag == null || "".equals(applyFlag)) {
            return null;
        }
        final StringBuilder flags = new StringBuilder("____________________");
        final List<String> applyFlags = split2List(applyFlag);
        if (applyFlags.contains("1")) {
            flags.replace(8, 9, "1");
        }
        if (applyFlags.contains("2")) {
            flags.replace(12, 13, "1");
        }
        if (applyFlags.contains("3")) {
            flags.replace(13, 14, "1");
        }
        if (applyFlags.contains("4")) {
            flags.replace(14, 15, "1");
        }
        if (applyFlags.contains("5")) {
            flags.replace(15, 16, "1");
        }
        if (applyFlags.contains("6")) {
            flags.replace(16, 17, "1");
        }
        return flags.toString();
    }

    public static String flag2itemFlag(final String flags) {
        if (flags == null) {
            return null;
        }
        final String[] attribute = flags.split("");
        String itemFlags = " ";
        if ("1".equals(attribute[0])) {
            itemFlags += ",0";
        }
        if ("2".equals(attribute[0])) {
            itemFlags += ",7";
        }
        if ("1".equals(attribute[1])) {
            itemFlags += ",1";
        }
        if ("1".equals(attribute[2])) {
            itemFlags += ",2";
        }
        if ("1".equals(attribute[3])) {
            itemFlags += ",3";
        }
        if ("1".equals(attribute[4])) {
            itemFlags += ",4";
        }
        if ("1".equals(attribute[5])) {
            itemFlags += ",5";
        }
        if ("1".equals(attribute[6])) {
            itemFlags += ",6";
        }
        if ("1".equals(attribute[7])) {
            itemFlags += ",8";
        }
        if ("1".equals(attribute[8])) {
            itemFlags += ",9";
        }
        if ("2".equals(attribute[8])) {
            itemFlags += ",21";
        }
        if ("3".equals(attribute[8])) {
            itemFlags += ",22";
        }
        if ("1".equals(attribute[9])) {
            itemFlags += ",10";
        }
        if ("1".equals(attribute[10])) {
            itemFlags += ",11";
        }
        if ("1".equals(attribute[11])) {
            itemFlags += ",12";
        }
        if ("2".equals(attribute[11])) {
            itemFlags += ",13";
        }
        return itemFlags;
    }

    public static String flag2String(final String flags) {
        if (flags == null) {
            return null;
        }
        final String[] attribute = flags.split("");
        String itemFlags = " ";
        if ("1".equals(attribute[0])) {
            itemFlags += ",不允许预约";
        }
        if ("2".equals(attribute[0])) {
            itemFlags += ",只允许医技前台预约";
        }
        if ("1".equals(attribute[1])) {
            itemFlags += ",镇静";
        }
        if ("1".equals(attribute[2])) {
            itemFlags += ",增强";
        }
        if ("1".equals(attribute[3])) {
            itemFlags += ",空腹";
        }
        if ("1".equals(attribute[4])) {
            itemFlags += ",憋尿";
        }
        if ("1".equals(attribute[5])) {
            itemFlags += ",穿刺";
        }
        if ("1".equals(attribute[6])) {
            itemFlags += ",不允许自助报到";
        }
        if ("1".equals(attribute[7])) {
            itemFlags += ",不允许临床预约";
        }
        if ("2".equals(attribute[7])) {
            itemFlags += ",不允许临床门诊";
        }
        if ("3".equals(attribute[7])) {
            itemFlags += ",不允许临床住院";
        }
        if ("1".equals(attribute[8])) {
            itemFlags += ",不允许自助预约";
        }
        if ("1".equals(attribute[9])) {
            itemFlags += ",不允许移动预约";
        }
        if ("1".equals(attribute[10])) {
            itemFlags += ",普通";
        }
        if ("1".equals(attribute[11])) {
            itemFlags += ",筛查";
        }
        if ("2".equals(attribute[11])) {
            itemFlags += ",NT";
        }
        if (itemFlags == null) {
            return null;
        }
        final String str = itemFlags.substring(1);
        return str;
    }

    public static List<String> examItemFlag(final String flags) {
        final List<String> itemFlags = new ArrayList<String>();
        if (StringUtils.isBlank(flags)) {
            return itemFlags;
        }
        final String key = "1";
        int begIndex = 0;
        if (flags.charAt(0) == '2') {
            itemFlags.add("7");
            begIndex = 1;
        }
        for (int index = flags.indexOf(key, begIndex); index != -1; index = flags.indexOf(key, index + 1)) {
            itemFlags.add(String.valueOf(index));
        }
        return itemFlags;
    }

    public static List<String> getJsonNode(final String jsonStr, final String path) {
        final List<String> list = new ArrayList<String>();
        if (StringUtils.isBlank(jsonStr)) {
            return list;
        }
        Object obj = null;
        try {
            obj = JSONPath.read(jsonStr, path);
        } catch (Exception e) {
            CommonUtil.log.error("节点为【" + path + "】规则格式错误,请检查");
        }
        if (obj == null) {
            return null;
        }
        if (obj instanceof String) {
            list.add((String) obj);
            return list;
        }
        if (obj instanceof List) {
            final Object[] array;
            final Object[] objs = array = ((List) obj).toArray();
            for (final Object o : array) {
                list.add(String.valueOf(o));
            }
            return list;
        }
        if (obj instanceof Integer) {
            list.add(String.valueOf(obj));
            return list;
        }
        if (obj instanceof Object) {
            list.add(String.valueOf(obj));
            return list;
        }
        return list;
    }

    public static String getJsonNode0(final String jsonStr, final String path) {
        final List<String> list = getJsonNode(jsonStr, path);
        String val = "";
        if (list != null && list.size() > 0) {
            val = list.get(0);
        }
        return (val != null) ? val : "";
    }

    public static Map<String, String> stringToMap(final String str) {
        final String[] arr = str.split(",");
        final Map<String, String> map = new HashMap<String, String>();
        for (final String a : arr) {
            final String[] array = a.split("=");
            if (array.length > 1) {
                map.put(array[0].toLowerCase(), array[1]);
            }
        }
        return map;
    }

    public static String replaceMsg(String pattern, final HashMap<String, Object> mapInfo) {
        for (final Object obj : mapInfo.keySet()) {
            if (obj == null) {
                continue;
            }
            final Object key = obj;
            Object value = mapInfo.get(key);
            if (value == null) {
                value = "";
            }
            String strKey = key.toString().toLowerCase();
            pattern = pattern.replace("#" + strKey, value.toString());
            strKey = strKey.toUpperCase();
            pattern = pattern.replace("#" + strKey, value.toString());
        }
        return pattern;
    }

    public static String convertTerminalType(final String terminalType) {
        switch (terminalType) {
            case "A":
            case "01":
            case "DEPT": {
                return "医技预约";
            }
            case "B":
            case "02":
            case "CLINIC_IN": {
                return "临床预约";
            }
            case "C":
            case "03":
            case "SELF": {
                return "自助终端";
            }
            case "D":
            case "04":
            case "MOBILE": {
                return "移动终端";
            }
            case "E":
            case "05": {
                return "预约中心";
            }
            case "F": {
                return "预约管理";
            }
            case "G": {
                return "系统";
            }
            case "H":
            case "06":
            case "CLINIC_OUT": {
                return "临床门诊";
            }
            case "R":
            case "99": {
                return "绿色通道";
            }
            case "DEFAULT": {
                return "通用";
            }
            default: {
                return "";
            }
        }
    }

    public static String ReminderType(final String reminderType) {
        switch (reminderType) {
            case "REMINDER_SCHEDULE": {
                return "预约凭条";
            }
            case "REMINDER_SIGN": {
                return "报到凭条";
            }
            default: {
                return "";
            }
        }
    }

    public static String[] reverseArray(final String[] Array) {
        final String[] new_array = new String[Array.length];
        for (int i = 0; i < Array.length; ++i) {
            new_array[i] = Array[Array.length - i - 1];
        }
        return new_array;
    }

    public static String turnApmFlag(final String apmFlag) {
        if (StringUtils.isBlank(apmFlag)) {
            return "全天";
        }
        switch (apmFlag) {
            case "0": {
                return "上午";
            }
            case "1": {
                return "下午";
            }
            case "2": {
                return "晚上";
            }
            case "3": {
                return "全天";
            }
            case "4": {
                return "中午";
            }
            default: {
                return "全天";
            }
        }
    }

    public static String convertSex(final String sex) {
        String result = "";
        if (StringUtils.isNotBlank(sex)) {
            switch (sex) {
                case "0": {
                    result = "男";
                    break;
                }
                case "1": {
                    result = "女";
                    break;
                }
                default: {
                    result = "未知";
                    break;
                }
            }
        }
        return result;
    }

    public static String convertPatientSource(final String patientSource) {
        String result = "";
        if (StringUtils.isNotBlank(patientSource)) {
            switch (patientSource) {
                case "0": {
                    result = "门诊";
                    break;
                }
                case "1": {
                    result = "住院";
                    break;
                }
                case "2": {
                    result = "体检";
                    break;
                }
                case "3": {
                    result = "职工";
                    break;
                }
                default: {
                    result = patientSource;
                    break;
                }
            }
        }
        return result;
    }

    public static List<String> mergeNo2ApplyNos(final String mergeNoStr) {
        final List<String> applyNos = new ArrayList<String>();
        if (StringUtils.isBlank(mergeNoStr)) {
            return applyNos;
        }
        final String[] split;
        final String[] mergeNoArr = split = mergeNoStr.split(",");
        for (final String mergeNo : split) {
            final int idx = mergeNo.lastIndexOf("-");
            final String applyNo = mergeNo.substring(0, idx);
            if (!applyNos.contains(applyNo)) {
                applyNos.add(applyNo);
            }
        }
        return applyNos;
    }

    public static String toDecimal(long n) {
        StringBuilder sb = new StringBuilder(8);
        final char[] b = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
        while (n != 0L) {
            sb = sb.append(b[(int) (n % 16L)]);
            n /= 16L;
        }
        final String a = sb.reverse().toString();
        return a;
    }

    public List<String> getSickIdsCondition(final String sickId) {
        List<String> list = null;
        if (StringUtils.isNotBlank(sickId)) {
            list = new ArrayList<String>();
            final String[] split;
            final String[] arr = split = sickId.split(",");
            for (final String tmp : split) {
                list.add(tmp);
            }
        }
        return list;
    }

    public List<String> getExamClassesCondition(final String examClass) {
        List<String> list = null;
        if (StringUtils.isNotBlank(examClass)) {
            list = new ArrayList<String>();
            String[] arr = examClass.split(",");
            if (arr.length <= 1) {
                arr = examClass.split("\\|");
            }
            if (arr.length <= 1) {
                arr = examClass.split("、");
            }
            for (final String tmp : arr) {
                list.add(tmp);
            }
        }
        return list;
    }

    public List<String> getReqDeptCondition(final String reqDept) {
        List<String> list = null;
        if (StringUtils.isNotBlank(reqDept)) {
            list = new ArrayList<String>();
            String[] arr = reqDept.split(",");
            if (arr.length <= 1) {
                arr = reqDept.split("\\|");
            }
            if (arr.length <= 1) {
                arr = reqDept.split("、");
            }
            for (final String tmp : arr) {
                list.add(tmp);
            }
        }
        return list;
    }

    public String getClinicPathFlag(String flags) {
        String clinicPathFlag = "0";
        if (StringUtils.isNotBlank(flags)) {
            flags = flags.trim();
            if (flags.trim().length() > 7) {
                clinicPathFlag = flags.trim().substring(7, 8);
            }
        }
        return clinicPathFlag;
    }

    public static String subFlags(final String flags, final int endIndex) {
        if (StringUtils.isNotBlank(flags)) {
            final int beginIndex = endIndex - 1;
            final String str = flags.substring(beginIndex, endIndex);
            return str;
        }
        return "";
    }

    public static <T, F> List<F> listCopyToAnottherList(final Class<F> target, final List<T> sourceList) {
        if (!CollectionUtils.isEmpty(sourceList)) {
            final List<F> targetList = new ArrayList<F>();
            for (final T t : sourceList) {
                try {
                    final F f = target.newInstance();
                    BeanUtils.copyProperties((Object) t, (Object) f);
                    targetList.add(f);
                } catch (Exception e) {
                    CommonUtil.log.error(e.getMessage());
                }
            }
            return targetList;
        }
        return null;
    }

    public static String toString4LimitType(final String limitType) {
        String result = "";
        if (StringUtils.isNotBlank(limitType)) {
            switch (limitType) {
                case "1": {
                    result = "，单数限制:";
                    break;
                }
                case "2": {
                    result = "，系数限制:";
                    break;
                }
                default: {
                    result = "，错误:";
                    break;
                }
            }
        }
        return result;
    }

    public static String toString4ScheduleDate(final String resourceMark, final String scheduleDate, final String weekDate) {
        String str = "";
        if ("日期".equals(resourceMark)) {
            str = "，预约日期：" + scheduleDate;
        } else if ("星期".equals(resourceMark)) {
            str = "，预约星期：" + convertWeek(weekDate);
        }
        return str;
    }

    public static String convertStopFlag(final String stopFlag) {
        String result = "";
        if (!org.springframework.util.StringUtils.isEmpty((Object) stopFlag)) {
            switch (stopFlag) {
                case "0": {
                    result = "启用";
                    break;
                }
                case "1": {
                    result = "停用";
                    break;
                }
                default: {
                    result = "其它";
                    break;
                }
            }
        }
        return result;
    }

    public static String convertApmFlag(final String apmFlag) {
        String result = "";
        if (!org.springframework.util.StringUtils.isEmpty((Object) apmFlag)) {
            switch (apmFlag) {
                case "0": {
                    result = "上午";
                    break;
                }
                case "1": {
                    result = "下午";
                    break;
                }
                case "2": {
                    result = "晚上";
                    break;
                }
                case "4": {
                    result = "中午";
                    break;
                }
                default: {
                    result = "其它";
                    break;
                }
            }
        }
        return result;
    }

    public static String convertWeek(final String weekDate) {
        String result = "";
        if (!org.springframework.util.StringUtils.isEmpty((Object) weekDate)) {
            switch (weekDate) {
                case "0": {
                    result = "星期日";
                    break;
                }
                case "1": {
                    result = "星期一";
                    break;
                }
                case "2": {
                    result = "星期二";
                    break;
                }
                case "3": {
                    result = "星期三";
                    break;
                }
                case "4": {
                    result = "星期四";
                    break;
                }
                case "5": {
                    result = "星期五";
                    break;
                }
                case "6": {
                    result = "星期六";
                    break;
                }
                default: {
                    result = "未知";
                    break;
                }
            }
        }
        return result;
    }

    public static String addDays2(final String date, final int m) {
        Date d = null;
        final Calendar c = Calendar.getInstance();
        try {
            d = new SimpleDateFormat("yy-MM-dd").parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        c.setTime(d);
        c.add(5, m);
        return new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
    }

    public static String convertSql(final String field1, String order1, final String field2, String order2) {
        String sortFieldOrderStr = "";
        if (StringUtils.isNotBlank(field1)) {
            final String fieldStr1 = turnField(field1);
            order1 = (StringUtils.isNotBlank(order1) ? order1 : "asc");
            final String[] split;
            final String[] fieldArr1 = split = fieldStr1.split("，");
            for (final String field3 : split) {
                sortFieldOrderStr = sortFieldOrderStr + "," + field3 + " " + order1;
            }
            if (StringUtils.isNotBlank(sortFieldOrderStr)) {
                sortFieldOrderStr = sortFieldOrderStr.substring(1);
            }
        }
        if (StringUtils.isNotBlank(field2)) {
            final String fieldStr2 = turnField(field2);
            order2 = (StringUtils.isNotBlank(order2) ? order2 : "asc");
            final String[] split2;
            final String[] fieldArr2 = split2 = fieldStr2.split("，");
            for (final String field3 : split2) {
                sortFieldOrderStr = sortFieldOrderStr + "," + field3 + " " + order2;
            }
            if (StringUtils.isNotBlank(sortFieldOrderStr) && sortFieldOrderStr.startsWith(",")) {
                sortFieldOrderStr = sortFieldOrderStr.substring(1);
            }
        }
        if (StringUtils.isBlank(sortFieldOrderStr)) {
            sortFieldOrderStr = "req_date asc,req_time asc";
        }
        return sortFieldOrderStr;
    }

    public static String turnField(final String field) {
        if (field == null) {
            return "";
        }
        switch (field) {
            case "0": {
                return "scheduled_date，scheduled_time";
            }
            case "1": {
                return "req_date，req_time";
            }
            case "2": {
                return "to_number(regexp_substr(queue_no,'[0-9]*[0-9]',1))";
            }
            case "3": {
                return "to_number(regexp_substr(bed_no,'[0-9]*[0-9]',1))";
            }
            case "4": {
                return "apply_no";
            }
            case "5": {
                return "visit_date，visit_time";
            }
            case "6": {
                return "exam_class";
            }
            case "7": {
                return "schedule_status";
            }
            case "8": {
                return "req_ward_name";
            }
            case "9": {
                return "req_dept_name";
            }
            default: {
                return "";
            }
        }
    }

    public static String turnFieldV2(final String field) {
        if (field == null) {
            return "reqDate";
        }
        switch (field) {
            case "0": {
                return "scheduleDate，scheduledTime";
            }
            case "1": {
                return "reqDate";
            }
            case "2": {
                return "queueNo";
            }
            case "3": {
                return "bedNo";
            }
            case "4": {
                return "applyNo";
            }
            case "5": {
                return "visitDate";
            }
            case "6": {
                return "examClass";
            }
            case "7": {
                return "scheduleStatus";
            }
            case "8": {
                return "reqWardCode";
            }
            case "9": {
                return "reqDept";
            }
            default: {
                return "reqDate";
            }
        }
    }

    public static String chargeFlag2Text(final String chargeFlag) {
        if (chargeFlag == null) {
            return "未缴费";
        }
        if ("1".equals(chargeFlag)) {
            return "已缴费";
        }
        return "未缴费";
    }

    public static String applyFlag2Text(final String type) {
        String result = "未知";
        if (StringUtils.isNotBlank(type)) {
            switch (type) {
                case "0": {
                    result = "是否需要提示注意事项";
                    break;
                }
                case "1": {
                    result = "HIS同步标识";
                    break;
                }
                case "2": {
                    result = "是否电子申请单";
                    break;
                }
                case "3": {
                    result = "是否孕妇";
                    break;
                }
                case "4": {
                    result = "是否哺乳期";
                    break;
                }
                case "5": {
                    result = "是否肾功能异常";
                    break;
                }
                case "6": {
                    result = "是否肝功能异常";
                    break;
                }
                case "7": {
                    result = "是否遵循临床路径";
                    break;
                }
                case "8": {
                    result = "是否床边检查";
                    break;
                }
                case "9": {
                    result = "是否VIP病人";
                    break;
                }
                case "10": {
                    result = "是否绝经";
                    break;
                }
                case "11": {
                    result = "是否病重";
                    break;
                }
                case "12": {
                    result = "是否无痛检查";
                    break;
                }
                case "13": {
                    result = "是否传染病";
                    break;
                }
                case "14": {
                    result = "是否本院建档";
                    break;
                }
                case "15": {
                    result = "是否镇静";
                    break;
                }
                default: {
                    result = "未知";
                    break;
                }
            }
        }
        return result;
    }

    public static List<String> ApplyFlag2String(final String flags) {
        if (flags == null) {
            return null;
        }
        final String[] attribute = flags.split("");
        final List<String> applyFlags = new ArrayList<String>();
        if ("1".equals(attribute[8])) {
            applyFlags.add("床边检查");
        }
        if ("1".equals(attribute[12])) {
            applyFlags.add("无痛检查");
        } else if ("2".equals(attribute[12])) {
            applyFlags.add("普通检查");
        }
        if ("1".equals(attribute[13])) {
            applyFlags.add("传染病");
        }
        if ("1".equals(attribute[14])) {
            applyFlags.add("本院建档");
        }
        if ("1".equals(attribute[15])) {
            applyFlags.add("镇静检查");
        }
        if ("1".equals(attribute[16])) {
            applyFlags.add("多重耐药");
        }
        return applyFlags;
    }

    public static List<String> convertNotice(List<String> itemNotices, String patientSource, String endTime, String birthDate) {
        List<String> notices = new ArrayList<String>();
        if (itemNotices.size() == 0 || org.apache.commons.lang3.StringUtils.isBlank((CharSequence) patientSource)) {
            return notices;
        }
        for (String notice : itemNotices) {
            String noticeStr = notice.replaceAll(" ", "");
            if (noticeStr.contains("<notices>") && birthDate != null) {
                noticeStr = noticeStr.replaceAll("<notices>", "");
                noticeStr = noticeStr.replaceAll("</notices>", "");
                StringBuilder sb = new StringBuilder();
                sb.append("<notices>");
                sb.append(noticeStr);
                sb.append("</notices>");
                int ageDays = DateUtils.getBirthDays(birthDate);
                com.alibaba.fastjson.JSONObject jsonObject = XmlUtil.xml2JSON(sb.toString());
                List<String> jsonNotice = getJsonNode(jsonObject.toJSONString(), "$.notice");
                for (String s : jsonNotice) {
                    Map<String, Object> map = JsonUtil.json2Map(s);
                    String text = Objects.nonNull(map.get("text")) ? map.get("text").toString() : "";
                    if (map.get("patientSource") != null) {
                        String patientSources = map.get("patientSource").toString();
                        if (!patientSources.contains(patientSource)) {
                            continue;
                        }
                    }
                    if (Objects.nonNull(map.get("minDays")) && Objects.nonNull(map.get("maxDays"))) {
                        int minDays = Integer.parseInt(map.get("minDays").toString());
                        int maxDays = Integer.parseInt(map.get("maxDays").toString());
                        if (ageDays < minDays) {
                            continue;
                        }
                        if (ageDays > maxDays) {
                            continue;
                        }
                    }
                    noticeStr = text;
                    String[] split;
                    String[] arr = split = noticeStr.split("\\|");
                    for (String n : split) {
                        if (org.apache.commons.lang3.StringUtils.isNotBlank((CharSequence) n) && !notices.contains(n)) {
                            notices.add(n);
                        }
                    }
                    break;
                }
            } else {
                String noticeKey = "【" + patientSource + "】";
                int beginIndex = 0;
                int endIndex = 0;
                if (noticeStr.contains(noticeKey)) {
                    beginIndex = notice.indexOf(noticeKey) + noticeKey.length();
                    beginIndex = notice.indexOf("=", beginIndex) + 1;
                    endIndex = notice.indexOf("&【", beginIndex);
                    if (endIndex != -1) {
                        noticeStr = noticeStr.substring(beginIndex, endIndex);
                    } else {
                        noticeStr = noticeStr.substring(beginIndex);
                    }
                }
                String noticeKey2 = "<<";
                String noticeKey3 = ">>";
                if (org.apache.commons.lang3.StringUtils.isNotBlank((CharSequence) endTime) && noticeStr.contains(noticeKey2) && noticeStr.contains(noticeKey3)) {
                    beginIndex = noticeStr.indexOf(noticeKey2) + noticeKey2.length();
                    endIndex = noticeStr.indexOf(">>", beginIndex);
                    String tempNotice = noticeStr.substring(beginIndex, endIndex);
                    String[] split2;
                    String[] arr2 = split2 = tempNotice.split("\\^");
                    for (String str : split2) {
                        String[] arr3 = str.split("=");
                        String tempTime = arr3[0];
                        String tempStartTime = tempTime.split("-")[0];
                        tempStartTime = DateUtils.format(tempStartTime, "HH:mm");
                        String tempEndTime = tempTime.split("-")[1];
                        tempEndTime = DateUtils.format(tempEndTime, "HH:mm");
                        if ("00:00".equals(tempEndTime)) {
                            tempEndTime = "24:00";
                        }
                        if (tempStartTime.compareTo(endTime) <= 0 && tempEndTime.compareTo(endTime) >= 0) {
                            noticeStr = noticeStr.replace(noticeKey2 + tempNotice + noticeKey3, arr3[1]);
                            break;
                        }
                    }
                }
                String[] split3;
                String[] arr4 = split3 = noticeStr.split("\\|");
                for (String n2 : split3) {
                    if (org.apache.commons.lang3.StringUtils.isNotBlank((CharSequence) n2) && !notices.contains(n2)) {
                        notices.add(n2);
                    }
                }
            }
        }
        return notices;
    }


    public static String json2Xml(String jsonStr) throws JSONException {
        JSONObject jsonObj = new JSONObject(jsonStr);
        String xmlStr = XML.toString((Object) jsonObj);
        return xmlStr;
    }


    public static void main(final String[] args) {
        final String s = "<notices>\n\t<notice>\n\t\t<text>前2天18:00注意事项</text>\n\t\t<smsDays>2</smsDays>\n\t\t<smsTime>18:00</smsTime>\n\t\t<smsApmStartTime>08:00</smsApmStartTime>\n\t\t<smsApmEndTime>12:00</smsApmEndTime>\n\t\t<smsContent>明天早、中餐 可进食：米粥、烂糊面、蛋羹、鱼片、藕粉；禁食：米饭、蔬菜、其他肉类、带子水果;晚餐可进食：无渣食物，如肉汤、粥水、果汁、糖水、运动饮料；禁食：有渣食物、牛奶、豆浆。</smsContent>\n\t</notice>\n\t<notice>\n\t\t<text>前1天18:00注意事项</text>\n\t\t<smsDays>1</smsDays>\n\t\t<smsTime>18:00</smsTime>\t\n\t\t<smsApmStartTime>08:00</smsApmStartTime>\n\t\t<smsApmEndTime>12:00</smsApmEndTime>\n\t\t<smsContent>明天早餐禁食,检查前2小时禁饮水</smsContent>\n\t</notice>\n\t<notice>\n\t\t<text>前1天20:00注意事项</text>\n\t\t<smsDays>1</smsDays>\n\t\t<smsTime>20:00</smsTime>\n\t\t<smsApmStartTime>08:00</smsApmStartTime>\n\t\t<smsApmEndTime>12:00</smsApmEndTime>\n\t\t<smsContent>凌晨4:00时起用聚乙 二醇2包泡温开水2000毫升，搅拌至完全溶解，1至2小时内分次喝完。//口服20%甘露醇250mL，10min后饮水1500-2000L。喝泻药后多走动，注意观察大便次数、性状，是否已排清。</smsContent>\n\t</notice>\n\t<notice>\n\t\t<text>当天7:00注意事项</text>\n\t\t<smsDays>0</smsDays>\n\t\t<smsTime>07:00</smsTime>\t\n\t\t<smsApmStartTime>08:00</smsApmStartTime>\n\t\t<smsApmEndTime>12:00</smsApmEndTime>\n\t\t<smsContent>您今天上午将行内镜诊疗，贵重物品（手机、钱、首饰等）禁止带入检查室。</smsContent>\n\t</notice>\n\t<notice>\n\t\t<text>前2天18:00注意事项</text>\n\t\t<smsDays>2</smsDays>\n\t\t<smsTime>18:00</smsTime>\n\t\t<smsApmStartTime>14:30</smsApmStartTime>\n\t\t<smsApmEndTime>17:00</smsApmEndTime>\n\t\t<smsContent>明天可进食：米粥、烂糊面、蛋羹、鱼片、藕粉；禁食：米饭、蔬菜、其他肉类、带子水果、牛奶、豆浆</smsContent>\n\t</notice>\n\t<notice>\n\t\t<text>前1天20:00注意事项</text>\n\t\t<smsDays>1</smsDays>\n\t\t<smsTime>20:00</smsTime>\t\n\t\t<smsApmStartTime>14:30</smsApmStartTime>\n\t\t<smsApmEndTime>17:00</smsApmEndTime>\n\t\t<smsContent>明天8点前可吃清汤或无色糖水、运动饮料，8点后禁食，检查前2小时禁饮水。</smsContent>\n\t</notice>\n\t<notice>\n\t\t<text>当天07:00注意事项</text>\n\t\t<smsDays>0</smsDays>\n\t\t<smsTime>07:00</smsTime>\n\t\t<smsApmStartTime>14:30</smsApmStartTime>\n\t\t<smsApmEndTime>17:00</smsApmEndTime>\n\t\t<smsContent>上午10:00时起用聚乙 二醇2包泡温开水2000毫升，搅拌至完全溶解，1至2小时内分次喝完。//口服20%甘露醇250mL，10min后饮水1500-2000L。喝泻药后多走动，注意观察大便次数、性状，是否已排清。</smsContent>\n\t</notice>\n\t<notice>\n\t\t<text>当天12:00注意事项</text>\n\t\t<smsDays>0</smsDays>\n\t\t<smsTime>12:00</smsTime>\t\n\t\t<smsApmStartTime>14:30</smsApmStartTime>\n\t\t<smsApmEndTime>17:00</smsApmEndTime>\n\t\t<smsContent>您今天下午将行内镜诊疗，贵重物品（手机、钱、首饰等）禁止带入检查室。</smsContent>\n\t</notice>\n</notices>";
    }

    static {
        log = LoggerFactory.getLogger((Class) CommonUtil.class);
    }
}
